package server;

import java.util.Calendar;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;

public class ExpireAuctionsTask extends TimerTask {

	@Override
	public void run() {
		CopyOnWriteArrayList<Auction> auctions = ServerManager.getInstance()
				.getRunningAuctions();
		CopyOnWriteArrayList<User> users = ServerManager.getInstance().getUsers();
		
		Calendar now = Calendar.getInstance();
		
		for (Auction a : auctions) {
			if (now.after(a.getDeadline())) {
				String bidder = (a.getHighestBidder() == null) ? "n/a" : a.getHighestBidder().getName();
				String msg = "!auction-ended " + bidder + " " + a.getHighestBid() + " " + a.getDescription();
				for (User u : users) {
					UdpSender.getInstance().sendMessage(msg, u);
				}
				
				ServerManager.getInstance().getRunningAuctions().remove(a);
			}
		}

	}

}
